const { readFileSync, writeFileSync } = require('fs')


function getIPAdress() {
    var interfaces = require('os').networkInterfaces();
    for (var devName in interfaces) {
        var iface = interfaces[devName];
        for (var i = 0; i < iface.length; i++) {
            var alias = iface[i];
            if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
                return alias.address;
            }
        }
    }
}

module.exports = (url) => {
    const content = readFileSync(url).toString()
    const data = JSON.parse(content)
    const ip = getIPAdress()

    for (const key in data) {
        data[key] = data[key].map((item, index) => {
            item.img = `http://${ip}:8888/images/${key + (index + 1)}.jpg`
            return item
        })
    }
    writeFileSync(url, JSON.stringify(data))

}
